//
//  PhotoBrowerAnimator.swift
//  JIMWeiBo
//
//  Created by 陈恒均 on 2018/2/25.
//  Copyright © 2018年 陈恒均. All rights reserved.
//

import UIKit

//面向协议开发
protocol AnimatorForPresentDelegate : NSObjectProtocol {
    func startRect(indexPath : NSIndexPath) -> CGRect
    func endRect(indexPath : NSIndexPath) -> CGRect
    func imageView(indexPath : NSIndexPath) -> UIImageView
}

protocol AnimatorDismissDelegate : NSObjectProtocol {
    func indexPathForDimissView() -> NSIndexPath
    func imageViewForDimissView() -> UIImageView
}

class PhotoBrowerAnimator: NSObject {
    var isPresented : Bool = false
    
    var presentedDelegate : AnimatorForPresentDelegate?
    var dismissDelegate : AnimatorDismissDelegate?
    var indexPath : NSIndexPath?
    
}

extension PhotoBrowerAnimator : UIViewControllerTransitioningDelegate{
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}

extension PhotoBrowerAnimator : UIViewControllerAnimatedTransitioning
{
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(transitionContext: transitionContext) : animationForDismissView(transitionContext: transitionContext)
    }
    
    func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentedDelegate = presentedDelegate, let indexPath = indexPath else {
            return
        }
        
        let presentView = transitionContext.view(forKey: UITransitionContextViewKey.to)
        
        transitionContext.containerView.addSubview(presentView!)
        
        //获取执行动画的imageView
        let startRect = presentedDelegate.startRect(indexPath: indexPath)
        let imageView = presentedDelegate.imageView(indexPath: indexPath)
        transitionContext.containerView.addSubview(imageView)
        imageView.frame = startRect
        
        presentView?.alpha = 0.0
        
        transitionContext.containerView.backgroundColor = UIColor.black
        
        presentView?.alpha = 0.0
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            imageView.frame = presentedDelegate.endRect(indexPath: indexPath)
       
        }) { (_) in
            presentView?.alpha = 1.0
            transitionContext.containerView.backgroundColor = UIColor.clear
            imageView.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
        
    }
    func animationForDismissView(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let dismissDelegate = dismissDelegate, let presentedDelegate = presentedDelegate else {
            return
        }
        
        let dismissView = transitionContext.view(forKey: .from)
        
//        transitionContext.containerView.addSubview(dismissView!)
        dismissView?.removeFromSuperview()
        
        let imageView = dismissDelegate.imageViewForDimissView()
        transitionContext.containerView.addSubview(imageView)
        let indexPath = dismissDelegate.indexPathForDimissView()

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
//            dismissView?.alpha = 0.0
            imageView.frame = presentedDelegate.startRect(indexPath: indexPath)
        }) { (_) in
            transitionContext.completeTransition(true)
        }
        
    }
    
    
    
}
